/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.database.metamodel;

import com.jb.framework.DataSourceManager;
import com.jb.framework.database.SelectDBCommand;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * BaseDatabaseEntry is the base class for all classes in metamodel package. It
 * should be inherited by sub classes that implement assign method to 
 * populate itself from a ResultSet.
 */
public abstract class BaseDatabaseEntity extends TreeNodeBase {
    private transient Log log = LogFactory.getLog(getClass());
    /**
     * Populates instance from a result set.
     * @param crs the ResultSet
     * @throws SQLException if db error occurs.
     */
    public abstract void assign(ResultSet crs) throws SQLException;
    /**
     * Returns a list of children objects by type. 
     * @param type the class of children.
     * @return list of object of the class.
     */
    public List getChildrenByType(Class type) {
        Vector list = new Vector();
        Iterator iter = getChildren().iterator();

        while (iter.hasNext()) {
            TreeNodeBase bc = (TreeNodeBase) iter.next();

            if (type.isInstance(bc)) {
                list.addElement(bc);
            }
        }

        return list;
    }
    /**
     * Returns the database this metadata is based on.
     * @return the database.
     */
    public Database getMyDatabase() {
        return (this instanceof Database) ? (Database) this : ((BaseDatabaseEntity) this.getParent()).getMyDatabase();
    }
    /**
     * Returns its name as a String.
     * @return object name.
     */
    public String toString() {
        return getName();
    }
    /**
     * Executes a SQL statement.
     * @param cmd the statement to be executed.
     */
    protected void executeSQL(String cmd) {
        ResultSet rs = null;
        Connection conn = null;
        this.getChildren().clear();

        try {
            conn = DataSourceManager.getConnection(this);

            SelectDBCommand stmt = new SelectDBCommand(cmd, null);
            stmt.execute(conn);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                    if(log.isErrorEnabled())
                        log.error(this,e);
                }
            }

            if (conn != null) {
                DataSourceManager.releaseConnection(this, conn);
            }
        }
    }
}
